import { Universal } from "koishi"

export const name = "bot-status-update-notify"

export function apply(ctx) {
  const m = new Map()
  ctx.on('bot-status-updated', bot => {
    if (!["discord", "minecraft"].includes(bot.platform)) return
    if (!bot.selfId) return

    if (m.has(bot.sid)) {
      m.get(bot.sid)()
      m.delete(bot.sid)
      if (bot.status === Universal.Status.ONLINE) return
    }
    m.set(bot.sid, ctx.setTimeout(() => {
      m.delete(bot.sid)
      ctx.bots["onebot:2748080608"]?.sendPrivateMessage("3470524928", [bot.platform + ":" + bot.selfId + " " + Universal.Status[bot.status]])
    }, bot.status === Universal.Status.ONLINE ? 100 : 30_000))
  })
}
